//
//  HMStatus.swift
//  sinaweibo
//
//  Created by WGP on 16/4/12.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

class HMStatus: NSObject {
    
//    https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00oqxoiD2thVQC58989b5f8eZTgogC
    
//    缓存cell的行高
    var rowHeight:CGFloat?
    
    ///微博创建时间
    var created_at: String?
    
    ///微博ID
    var id: Int64 = 0

    ///	微博信息内容
    var text: String?
    {
        didSet
        {
            attrText = text?.emoticonNameTextToPicAttText()
        }
    }
    
    var attrText: NSAttributedString?
    
    ///微博来源
    var source: String?
    {
        // 当KVC设置来源:<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>
        
        didSet
        {
                if let newSource = source
            {
                let firstRange = newSource.rangeOfString("\">")
                let lastRange = newSource.rangeOfString("</a>")
                
                guard let startIndex = firstRange?.endIndex else
                {
                    return
                }
                guard let endIndex = lastRange?.startIndex else
                {
                    return
                }
                
                source = "来自 " + newSource.substringWithRange(startIndex..<endIndex)
            }
        }
    }
    
    ///微博配图
    var pic_urls: [[String: AnyObject]]?
    {
        didSet
        {
            let count = pic_urls?.count ?? 0
            
            if count == 0
            {
                return
            }
            
            pic_urlsArr = [NSURL]()
            
//            有图片要显示
            for dict in pic_urls!
            {
                let urlString = dict["thumbnail_pic"] as! String
                let url = NSURL(string: urlString)!
                
                pic_urlsArr?.append(url)
            }
        }
    }
    
//    模型提供好用一点的图片的NSURL数组
    var pic_urlsArr: [NSURL]?
    
    
//    user模型
    var user: HMUser?
    
/// 转发的微博
    var retweeted_status:HMStatus?
    
    ///字典转模型
    init(dict:[String:AnyObject]) {
        
        super.init()
        
        
        setValuesForKeysWithDictionary(dict)
    }
    

    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
 /// 重写setvalue:forkey的方法,setValuesForKeysWithDictionary内部也是调用setvalue:forkey的方法,传入的字典有多少key就调用多少次,对于嵌套模型,可以在这个方法里拦截到对应的key,再进行字典转模型,对不是想要的key要记得调用父类的方法来处理.注意:要记得拦截处理后要记得返回,否则又调用父类的方法
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user",let userDict = value as? [String:AnyObject]
        {
            
            user = HMUser(dict: userDict)
            return
        }
        
        if key == "retweeted_status", let dict = value as? [String:AnyObject]
        {
            retweeted_status = HMStatus(dict: dict)
            return 
        }
        
        super.setValue(value, forKey: key)
    }
    
    ///对象的打印
    override var description: String
    {
        get
        {
            let keys = ["created_at", "id", "text", "source", "pic_urls"]
            
            return dictionaryWithValuesForKeys(keys).description
        }
    }
    
}







